
<template>
  <div class="goods">
    <el-card>
      <div class="top">
        <el-row>
          <el-col :span="12">
            <el-input
              placeholder="请输入内容"
              v-model="searchVal"
              class="input-with-select"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
              ></el-button> </el-input
          ></el-col>
          <el-col :span="6"
            ><el-button type="primary" @click="$router.push('/addgoods')"
              >添加商品</el-button
            >
          </el-col>
          <el-col :span="6"
            ><el-button type="primary">导出excel表格</el-button></el-col
          >
        </el-row>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.upd_time | time }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="handleEdit(scope.row)"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="delUser(scope.row)"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="el-pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { goodsList } from "../../http/goods";
export default {
  name: "",
  components: {},
  data() {
    return {
      searchVal: "", //搜索框
      tableData: [],
      page: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.render();
  },
  filters: {
    time(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
  mounted() {},
  methods: {
    render() {
      goodsList({ params: this.page }).then((res) => {
        console.log(res);
        this.tableData = res.data.goods;
        this.total = res.data.total;
      });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.render();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pagenum = val;
      this.render();
    },
  },
};
</script>
<style scoped lang='scss'>
.goods {
  .el-card {
    margin-top: 20px;
    .top {
      text-align: center;
    }
  }
}
</style>
